package com.example.bookKeep.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.example.bookKeep.entity.Book;
import com.example.bookKeep.entity.Process;
import com.example.bookKeep.entity.ProcessDetail;
import com.example.bookKeep.request.BookListRequest;
import com.example.bookKeep.service.ProcessDetailService;
import com.example.bookKeep.service.ProcessService;
import com.example.bookKeep.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/process")
@Slf4j
public class ProcessController {

    @Autowired
    private ProcessService processService;
    @Autowired
    private ProcessDetailService processDetailService;


    @PostMapping("/add")
    public void add(@RequestBody Process process) {
        if (process.getId() == null) {
            process.setCreateTime(new Date());
        }
        process.setUpdateTime(new Date());
        processService.saveOrUpdate(process);

    }

    @GetMapping("/listPurchaser")
    public List<String> listPurchaser() {
        return processService.listPurchaser();
    }

    @PostMapping("/list")
    public LinkedHashMap<LocalDate,List<ProcessVO>> list(@RequestBody BookListRequest bookListRequest) {
        List<Process> processes = processService.listByRequest(bookListRequest);
        List<ProcessVO> processVOS = processes.stream().map(item -> {
            ProcessVO processVO = new ProcessVO();
            BeanUtils.copyProperties(item, processVO);
            return processVO;
        }).collect(Collectors.toList());
        LinkedHashMap<LocalDate, List<ProcessVO>> result = processVOS.stream().collect(Collectors.groupingBy(ProcessVO::getBookDate, LinkedHashMap::new, Collectors.toList()));

        return result;
    }

    @GetMapping("/detail/{id}")
    public Process detail(@PathVariable("id") Integer id) {
        return processService.getById(id);
    }

    @DeleteMapping("/removeById/{id}")
    public void removeById(@PathVariable("id") Integer id) {
        processService.removeById(id);
    }


    @GetMapping("/homeStatistics")
    public ProcessStatisticsVO homeStatistics() {
        return processService.homeStatistics();
    }

    @GetMapping("/monthStatistics")
    public ProcessStatisticsContainerVO monthStatistics(String ym) {
        return processService.monthStatistics(ym);
    }

    @GetMapping("/yearStatistics")
    public ProcessStatisticsContainerVO yearStatistics(String ym) {
        return processService.yearStatistics(ym);
    }

    @PostMapping("/listByPurchasers")
    public List<ProcessPurchaserVO> listByPurchasers(@RequestBody BookListRequest bookListRequest) {
        if (StrUtil.isNotEmpty(bookListRequest.getYm()) && !bookListRequest.getYm().contains("-")) {
            bookListRequest.setY(bookListRequest.getYm());
            bookListRequest.setYm("");
        }
        return processService.listByPurchasers(bookListRequest);
    }


    @GetMapping("/settlement")
    public void  settlement(String purchaser){
        processService.settlement(purchaser);
    }

}
